home *** CD-ROM | disk | FTP | other *** search
/ PC Elektro 3 / PC-Elektro-3-cd1.bin / KBan 2.0 / KBANSRC.LZH / SRC / PROG / GRID.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-09-28  |  5.5 KB  |  238 lines

  1. /*
  2.  * the class GRID
  3.  * Copyright (C) 1997 Kazutaka Hirata <khirata@jove.acs.unt.edu>
  4.  */
  5.  
  6. #include "stdafx.h"
  7.  
  8. #include "grid.h"
  9.  
  10. GRID::GRID(uint zoom)
  11. {
  12.   m_zoom = min(zoom, ZOOM_MAX);
  13.   m_mat_size = double(m_zoom) / double(DIS_DINCH);
  14.   m_pc_design_size.set(X_MAX * m_zoom / DIS_DINCH, Y_MAX * m_zoom / DIS_DINCH);
  15.  
  16.   m_ac_grid_origin     .set(0, 0);
  17.   m_ac_grid_origin_user.set(0, 0);
  18.   m_main_grid_width = DIS_DINCH;
  19.   m_sub_grid_width  = DIS_DINCH;
  20.  
  21.   m_snap = TRUE;
  22. }
  23.  
  24. void GRID::zoom_set(int zoom)
  25. {
  26.   m_zoom = min(zoom, ZOOM_MAX);
  27.   m_mat_size = double(m_zoom) / double(DIS_DINCH);
  28.   m_pc_design_size.set(X_MAX * m_zoom / DIS_DINCH, Y_MAX * m_zoom / DIS_DINCH);
  29. }
  30.  
  31. int GRID::zoom_get(void) const
  32. {
  33.   return m_zoom;
  34. }
  35.  
  36. const XY& GRID::get_pc_design_size(void) const
  37. {
  38.   return m_pc_design_size;
  39. }
  40.  
  41. void GRID::set_pc_win_size(const XY& pc_win_size)
  42. {
  43.   m_pc_win_size = pc_win_size;
  44. }
  45.  
  46. const XY& GRID::get_pc_win_size(void) const
  47. {
  48.   return m_pc_win_size;
  49. }
  50.  
  51. void GRID::snap_change(void)
  52. {
  53.   m_snap = m_snap ? false : true;
  54. }
  55.  
  56. void GRID::snap_set(uint snap)
  57. {
  58.   m_snap = snap;
  59. }
  60.  
  61. uint GRID::snap_get(void) const
  62. {
  63.   return m_snap;
  64. }
  65.  
  66. XYT GRID::distance_pc2ac(XYT pc) const
  67. {
  68.   return XYT(double(pc) / m_mat_size);
  69. }
  70.  
  71. XYT GRID::distance_ac2pc(XYT ac) const
  72. {
  73.   return XYT(double(ac) * m_mat_size);
  74. }
  75.  
  76. XY GRID::distance_ac2pc(const XY& ac) const
  77. {
  78.   return ac * m_mat_size;
  79. }
  80.  
  81. void GRID::snap(XY& ac) const
  82. {
  83.   ac = ac - m_ac_grid_origin;
  84.   ac = (ac + m_sub_grid_width / 2) / int(m_sub_grid_width) * m_sub_grid_width;
  85.   ac = ac + m_ac_grid_origin;
  86. }
  87.  
  88. void GRID::snap_min(XY& ac) const
  89. {
  90.   ac = ac - m_ac_grid_origin;
  91.   ac = ac / int(m_sub_grid_width) * m_sub_grid_width;
  92.   ac = ac + m_ac_grid_origin;
  93. }
  94.  
  95. void GRID::xy_pc2ac_core(const XY& pc, XY& ac) const
  96. {
  97.   // XY pc_comp = m_pc_win_base + pc;
  98.   XY pc_math;
  99.   pc_math.set_x(                       pc.x());
  100.   pc_math.set_y(m_pc_design_size.y() - pc.y());
  101.   ac = pc_math / m_mat_size;
  102. }
  103.  
  104. void GRID::xy_pc2ac_with_snap_on(const XY& pc, XY& ac) const
  105. {
  106.   xy_pc2ac_core(pc, ac);
  107.   snap(ac);
  108. }
  109.  
  110. void GRID::xy_pc2ac_with_snap_off(const XY& pc, XY& ac) const
  111. {
  112.   xy_pc2ac_core(pc, ac);
  113. }
  114.  
  115. void GRID::xy_pc2ac(const XY& pc, XY& ac) const
  116. {
  117.   if(m_snap) {
  118.     xy_pc2ac_with_snap_on(pc, ac);
  119.   } else {
  120.     xy_pc2ac_with_snap_off(pc, ac);
  121.   }
  122. }
  123.  
  124. void GRID::xy_ac2pc(const XY& ac, XY& pc) const
  125. {
  126.   XY pc_math = ac * m_mat_size;
  127.   pc.set_x(                       pc_math.x());
  128.   pc.set_y(m_pc_design_size.y() - pc_math.y());
  129. }
  130.  
  131. void GRID::update_grid_origin(void)
  132. {
  133.   int xr = m_ac_grid_origin_user.x() % m_main_grid_width;
  134.   int yr = m_ac_grid_origin_user.y() % m_main_grid_width;
  135.   xr -= m_main_grid_width;
  136.   yr -= m_main_grid_width;
  137.   m_ac_grid_origin = XY(xr, yr);
  138. }
  139.  
  140. void GRID::set_grid_origin(const XY& ac_grid_origin_user)
  141. {
  142.   m_ac_grid_origin_user = ac_grid_origin_user;
  143.   update_grid_origin();
  144. }
  145.  
  146. XY GRID::get_grid_origin(void) const
  147. {
  148.   return m_ac_grid_origin;
  149. }
  150.  
  151. void GRID::set_grid_width(uint grid_width, uint div)
  152. {
  153.   m_main_grid_width = grid_width;
  154.   m_sub_grid_width  = grid_width / div;
  155.   update_grid_origin();
  156. }
  157.  
  158. uint GRID::get_main_grid_width(void) const
  159. {
  160.   return m_main_grid_width;
  161. }
  162.  
  163. uint GRID::get_sub_grid_width(void) const
  164. {
  165.   return m_sub_grid_width;
  166. }
  167.  
  168. void GRID::set_center(const XY& /*ac_center*/)
  169. {
  170.   // XY pc_center = distance_ac2pc(ac_center);
  171.   // pc_center.set_y(m_pc_design_size.y() - pc_center.y());
  172.   // m_pc_win_base = pc_center - m_pc_win_size / 2;
  173. }
  174.  
  175. void GRID::zoom_in(XY& pc_win_base)
  176. {
  177.   uint zoom_old = m_zoom;
  178.   uint zoom_new = min(uint(ZOOM_MAX), m_zoom * 3 / 2);
  179.   if(zoom_old != zoom_new) {
  180.     XY pc_center = pc_win_base + m_pc_win_size / 2;
  181.     XY ac_center;
  182.     xy_pc2ac_with_snap_off(pc_center, ac_center);
  183.     zoom_set(zoom_new);
  184.     xy_ac2pc(ac_center, pc_center);
  185.     pc_win_base = pc_center - m_pc_win_size / 2;
  186.   }
  187. }
  188.  
  189. void GRID::zoom_out(XY& pc_win_base)
  190. {
  191.   uint zoom_old = m_zoom;
  192.   uint zoom_new = max(uint(ZOOM_MIN), m_zoom * 2 / 3);
  193.   if(zoom_old != zoom_new) {
  194.     XY pc_center = pc_win_base + m_pc_win_size / 2;
  195.     XY ac_center;
  196.     xy_pc2ac_with_snap_off(pc_center, ac_center);
  197.     zoom_set(zoom_new);
  198.     xy_ac2pc(ac_center, pc_center);
  199.     pc_win_base = pc_center - m_pc_win_size / 2;
  200.   }
  201. }
  202.  
  203. void GRID::zoom_window(const XY& ac_min_src, const XY& ac_max_src, XY& pc_win_base)
  204. {
  205.   XY ac_min = get_min(ac_min_src, ac_max_src);
  206.   XY ac_max = get_max(ac_min_src, ac_max_src);
  207.   if(ac_min != XY(X_MIN, Y_MIN) && ac_max != XY(X_MAX, Y_MAX)) {
  208.     XY ac_size = ac_max - ac_min;
  209.     if(m_pc_win_size.x() * ac_size.y() < m_pc_win_size.y() * ac_size.x()) {
  210.       zoom_set(m_pc_win_size.x() * DIS_DINCH / ac_size.x());
  211.     } else {
  212.       zoom_set(m_pc_win_size.y() * DIS_DINCH / ac_size.y());
  213.     }
  214.     XY ac_center = (ac_min + ac_max) / 2;
  215.     XY pc_center;
  216.     xy_ac2pc(ac_center, pc_center);
  217.     pc_win_base = pc_center - m_pc_win_size / 2;
  218.   }
  219. }
  220.  
  221. COLORREF GRID::get_grid_color  (void) const { return RGB(255, 255, 255); }
  222. COLORREF GRID::get_cursor_color(void) const { return RGB(255, 255, 255); }
  223. COLORREF GRID::get_target_color(void) const { return RGB(255, 255, 255); }
  224. COLORREF GRID::get_erase_color (void) const { return RGB(  0,   0,   0); }
  225.  
  226. COLORREF GRID::get_layer_color(int layer) const
  227. {
  228.   COLORREF layer_color_table[] = {
  229.     //  RRR, GGG, BBB
  230.     RGB(255,   0,   0),
  231.     RGB(  0, 255,   0),
  232.     RGB(  0,   0, 255),
  233.     RGB(255, 255,   0),
  234.     RGB(  0, 255, 255)
  235.   };
  236.   return layer_color_table[layer];
  237. }
  238.